package com.redstar.expenses;

import java.util.Calendar;
import java.util.Random;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class MyWidgetProvider extends AppWidgetProvider {
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {

		Expense exp = new Expense(Calendar.getInstance(),context);
        
		// Get all ids
		ComponentName thisWidget = new ComponentName(context,
				MyWidgetProvider.class);
		int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
		for (int widgetId : allWidgetIds) {
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
					R.layout.widget_layout);

			
			
			//Normal Add intent
			Intent AddIntent = new Intent(context, ModifyExpense.class);
			AddIntent.putExtra("Expense", exp);
			AddIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
			PendingIntent pendingAdd = PendingIntent.getActivity(context, 0, AddIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
			
			//Quick Add intent
			Intent QuickAddIntent = new Intent(context, QuickAdd.class);
			QuickAddIntent.putExtra("Expense", exp);
			QuickAddIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
			PendingIntent pendingQuickAdd = PendingIntent.getActivity(context, 1, QuickAddIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
			
			remoteViews.setOnClickPendingIntent(R.id.Add, pendingAdd);
			remoteViews.setOnClickPendingIntent(R.id.QuickAdd, pendingQuickAdd);
			appWidgetManager.updateAppWidget(widgetId, remoteViews);
		}		
	}
}
